package de.windeln.marketing.validation;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.google.common.base.Strings;

import de.windeln.marketing.campaign.model.RequestClientObject;

@Component
public class AnalyticsLinkGenerationError implements Validator{

	
	@Override
	public boolean supports(Class<?> clazz) {
		return clazz == RequestClientObject.class;
	}

	@Override
	public void validate(Object target, Errors errors) {
		RequestClientObject rco = (RequestClientObject) target;
		List<String> utms = new ArrayList<String>();
		utms.add(rco.getUtmCampaign());
		utms.add(rco.getUtmMedium());
		utms.add(rco.getUtmSource());
		boolean a = Strings.isNullOrEmpty(utms.get(0));
		boolean b = Strings.isNullOrEmpty(utms.get(1));
		boolean c = Strings.isNullOrEmpty(utms.get(2));
		if(utms.contains(null)){
			if(a & b & c){
			}else{
				errors.rejectValue("utmSource", "Analytics Link Generation", "Analytics Link can only be created when all UTM Fields are filled! So please fill in or leave all fields out!");
				errors.rejectValue("utmMedium", "Analytics Link Generation", "Analytics Link can only be created when all UTM Fields are filled! So please fill in or leave all fields out!");
				errors.rejectValue("utmCampaign", "Analytics Link Generation", "Analytics Link can only be created when all UTM Fields are filled! So please fill in or leave all fields out!");
			}
		}
	
		
	}
}
